
*------------------------------------------------------------------------------*
* EUROPEAN COUNCIL
*
* Additional specs:
*	1. Fallback: GOV_POS if non-aligned PM or President (or ideological data is missing)
*
* Version:	2016-10-28
* Changes:	2016-??-??: First release
*------------------------------------------------------------------------------*



use tmp_eu, clear
gen _____EUROPEAN_COUNCIL = .



* ----------------------
* PM's seniority, i.e. no of quarters of uninterrupted incumbency

sort g101 p201 g105 
split p123, gen(zs)
gen zs4 = 1 if p201==1
by g101: replace zs4 = zs4[_n-1] + 1 if zs1==zs1[_n-1] & p201==1


// -> Replace PM with President if selected
	
	local presoverpml "$presoverpm"	// "translate" global to local, which can be tokenized
	*di "`presoverpml'"
	tokenize `presoverpml' 			// tokenize, i.e. grab country codes

	local wordcount = wordcount("`presoverpml'") // count how many countries are specified
	*di `wordcount'

	gen iso = .
	gen presoverpm = .
	forvalues j = 1/`wordcount' {
	
	local country`j': word `j' of `presoverpml' // grab first, second, third etc. country of list
	di "`country`j''"

	gen tmpctry`j' = "`country`j''"

	capture: replace iso=36  if tmpctry`j'=="AUS"
	capture: replace iso=40  if tmpctry`j'=="AUT"
	capture: replace iso=56  if tmpctry`j'=="BEL"
	capture: replace iso=100 if tmpctry`j'=="BGR" | tmpctry`j'=="BUL"
	capture: replace iso=124 if tmpctry`j'=="CAN"
	capture: replace iso=191 if tmpctry`j'=="HRV" | tmpctry`j'=="CRO"
	capture: replace iso=196 if tmpctry`j'=="CYP"
	capture: replace iso=203 if tmpctry`j'=="CZE"
	capture: replace iso=208 if tmpctry`j'=="DNK"
	capture: replace iso=233 if tmpctry`j'=="EST"
	capture: replace iso=246 if tmpctry`j'=="FIN"
	capture: replace iso=250 if tmpctry`j'=="FRA"
	capture: replace iso=276 if tmpctry`j'=="DEU" | tmpctry`j'=="GER"
	capture: replace iso=300 if tmpctry`j'=="GRC"
	capture: replace iso=348 if tmpctry`j'=="HUN"
	capture: replace iso=352 if tmpctry`j'=="ISL" | tmpctry`j'=="ICE"
	capture: replace iso=372 if tmpctry`j'=="IRL" | tmpctry`j'=="IRE"
	capture: replace iso=380 if tmpctry`j'=="ITA"
	capture: replace iso=392 if tmpctry`j'=="JPN" | tmpctry`j'=="JAP"
	capture: replace iso=428 if tmpctry`j'=="LVA"
	capture: replace iso=440 if tmpctry`j'=="LTU"
	capture: replace iso=442 if tmpctry`j'=="LUX"
	capture: replace iso=470 if tmpctry`j'=="MLT"
	capture: replace iso=528 if tmpctry`j'=="NLD"
	capture: replace iso=554 if tmpctry`j'=="NZL"
	capture: replace iso=578 if tmpctry`j'=="NOR"
	capture: replace iso=616 if tmpctry`j'=="POL"
	capture: replace iso=620 if tmpctry`j'=="PRT"
	capture: replace iso=642 if tmpctry`j'=="ROU" | tmpctry`j'=="ROM"
	capture: replace iso=703 if tmpctry`j'=="SVK"
	capture: replace iso=705 if tmpctry`j'=="SVN"
	capture: replace iso=724 if tmpctry`j'=="ESP" | tmpctry`j'=="SPA"
	capture: replace iso=752 if tmpctry`j'=="SWE"
	capture: replace iso=756 if tmpctry`j'=="SWI"
	capture: replace iso=826 if tmpctry`j'=="UK" | tmpctry`j'=="GBR"
	capture: replace iso=840 if tmpctry`j'=="USA"
	
	replace presoverpm = 1 if iso == g101
}	
	sort g101 p502 g105
	replace zs4 = . 			if presoverpm == 1
	replace zs1 = p503			if presoverpm == 1
	replace zs4 = 1 			if p502==1 & presoverpm == 1
	replace zs4 = zs4[_n-1] + 1 if zs1==zs1[_n-1] & p502==1 & presoverpm == 1
//

by g101 g105, sort: egen pm_experience = max(zs4)
gen sqrt_pm_experience = sqrt(pm_experience)

lab var pm_experience "Seniority of Head of State"
lab var sqrt_pm_experience "sqrt(Seniority)"
drop zs* iso
capture: drop tmpctry*



* ----------------------
* Position of Permanent Presidency (since 2010)

gen tmppres = .
replace tmppres = $ideology if (g105>=tq(2010q1) & g105<=tq(2014q4)) & p101==21521	// van Rompuy (BEL: CD&V)
replace tmppres = $ideology if (g105>=tq(2015q1) & g105<=tq(2017q2)) & p101==92435	// Tusk (POL: OB)

by p601 g105, sort: egen perm_pres = max(tmppres)

lab var perm_pres "Position of Permanent President of the European Council"
drop tmppres
sort g101 g105 p101



* ----------------------
* Head of States' Position

gen zs1 = $ideology if p201==1 & $ideology != . // Position of PM
replace zs1 = . if presoverpm==1
replace zs1 = $ideology if presoverpm==1 & p502==1 & $ideology != .	// Position of President
by g101 g105, sort: egen headofstate = mean(zs1)

replace headofstate = GOV_POS if headofstate == .	// Fallback: Government Position if non-aligned PM or President

lab var headofstate "Position of Head of State"
lab var presoverpm	"Dummy: 1 = President's position instead of PM's one"


* ----------------------
* Reduce to country file (EU members only)
by p601 g105 g101, sort: drop if _n!=_N
drop if p601==.
save zs, replace



* ----------------------
* Standard Positions
by p601 g105, sort: egen eucou_mean		= mean(headofstate)
by p601 g105, sort: egen eucou_median	= median(headofstate)
by p601 g105, sort: egen eucou_min		= min(headofstate)
by p601 g105, sort: egen eucou_max		= max(headofstate)
gen eucou_unanimity = eucou_min + (eucou_max - eucou_min)/2
gen eucou_range =  eucou_max - eucou_min

gen zs2 = headofstate if p614==1		// Position of the "normal" presidency
by p601 g105, sort: egen eucou_pres = max(zs2)
drop zs*

label var eucou_mean 		"European Council: Mean position"
label var eucou_median 		"European Council: Median position"
label var eucou_min 		"European Council: Minimum position"
label var eucou_max 		"European Council: Maximum position"
label var eucou_unanimity	"European Council: Position under unanimity rule"
label var eucou_range 		"European Council: Range inbetween European Council"
label var eucou_pres		"European Council: Position of the Presidency"



* ----------------------
* BARGAINING


* Standardize pm experience: var/max(var)
quietly: summ sqrt_pm_experience
gen std_sqrt_pm_experience = sqrt_pm_experience/r(max)
lab var std_sqrt_pm_experience "Standardized value, i.e. var/max(var)"
* Standardize population and gdp
foreach var of varlist population gdp_cur {
	by g105, sort: egen sum_`var' = sum(`var')
	gen std_`var' = `var'/sum_`var'
	lab var std_`var' "p% of `var' (per quarter)"
}
drop sum_*
order pm_experience sqrt_pm_experience std*, after(population)


* Position 1: p% of population*position
gen zs1 = std_population * headofstate
by g105, sort: egen pos1 = sum(zs1)
lab var pos1 "PM's positions weighted by population"

* Position 2: p% of gdp*position
gen zs2 = std_gdp_cur * headofstate
by g105, sort: egen pos2 = sum(zs2)
lab var pos2 "PM's Positions weighted by GDP"

* Position 3: pm seniority*position
gen zs3 = std_sqrt_pm_experience
by g105, sort: egen zs3_wsum = sum(zs3)
gen zs4 = zs3 * headofstate
by g105, sort: egen zs4_pos = sum(zs4)
gen pos3 = zs4_pos/zs3_wsum
lab var pos3 "PM's Positions weighted by PM's seniority"


* Position 4a: Mean of Presidency-PM compromise (i.e. mean)
gen zs5 =  (eucou_pres + headofstate) /2
replace zs5 = . if p614==1
by g105, sort: egen pos4 = mean(zs5)
lab var pos4 "Average Bargaining Presidency-Other PMs"
drop zs*

* Position 4b: since 2010 -> (Rotating Presidency + Permanent President) and PM compromise
sort p601 g105 g101
local rotprespowerl "$rotprespower"	// "translate" global to local, which can be tokenized
*di "`rotprespowerl'"
gen pstrio = "`rotprespowerl'" if g105 >= tq(2010q1)
lab var pstrio "Power scores for presidency trio"
tokenize `rotprespowerl', parse(":")	// tokenize power scores for rotating presidency
*di "`1'"
*di "`3'"
*di "`5'"

gen zs_rot_pres = .
replace zs_rot_pres = `1' if p615 == 3	// current
replace zs_rot_pres = `3' if p615 == 2	// successor
replace zs_rot_pres = `5' if p615 == 1	// upcoming
by p601 g105, sort: egen zs_rot_total = sum(zs_rot_pres)
replace zs_rot_total = . if g105 < tq(2010q1)

gen zs = headofstate * (zs_rot_pres / zs_rot_total)	// weigh positions
by p601 g105, sort: egen eucou_pres2 = sum(zs)

replace eucou_pres2 = . if g105<tq(2010q1)
lab var eucou_pres2 "European Council: Position of Rotating Presidency"

clonevar eucou_permpres = perm_pres
lab var eucou_permpres	"European Council: Position of Permanent President"

gen zs1 = (eucou_pres2 + eucou_permpres) /2
gen zs5 = (zs1 + headofstate) /2
replace zs5 = . if p615!=.
by g105, sort: egen pos42 = mean(zs5)
lab var pos42 "Average Bargaining Rotating Presidency-Other PMs"
drop zs*



* ----------------------
* Apply weights if "power" is chosen for any time period
if	"$eu_council1" == "power" |	///
	"$eu_council2" == "power" |	///
	"$eu_council3" == "power" |	///
	"$eu_council4" == "power" |	///
	"$eu_council5" == "power" ///
{
		
	local pmweightl = "$pmweight"	// "translate" global to local, which can be tokenized
	*di "`pmweightl'"
	local weight2 = regexr("`pmweightl'",","," ")	// parse weights
	*di "`weight2'"
	
	gen tmp_pos1  = regexm("`weight2'","population")
	gen tmp_pos2  = regexm("`weight2'","gdp")
	gen tmp_pos3  = regexm("`weight2'","seniority")
	gen tmp_pos4  = regexm("`weight2'","presidency1")
	gen tmp_pos42 = regexm("`weight2'","presidency2")
	
	gen zs_pos1  = pos1  if tmp_pos1  == 1
	gen zs_pos2  = pos2  if tmp_pos2  == 1
	gen zs_pos3  = pos3  if tmp_pos3  == 1
	gen zs_pos4  = pos4  if tmp_pos4  == 1
		* if "presidency2" is selected
	replace zs_pos4 = pos4  if tmp_pos42 == 1 & pos42 == .	// "normal" position until 2009
	replace zs_pos4 = pos42 if tmp_pos42 == 1 & pos42 != .	// rotating presidency since 2010

	sort p601 g105 g101
	egen eucou_power = rowmean(zs_pos*)
	lab var eucou_power "European Council: Position taking (structural) power into account"
	drop tmp* zs*
}



* ----------------------
* Model Choice

gen EUCOU_POS = .
gen european_council = ""

lab var EUCOU_POS 		 "EU: Position of European Council"
lab var european_council "Your model choice for the European Council"


// "Luxembourg compromise", consultation ( -1987q2)
	if 	"$eu_council1" == "mean" {
		replace EUCOU_POS = eucou_mean				if g105<=tq(1987q2)
		replace european_council = "$eu_council1"	if g105<=tq(1987q2)
	}
	else if "$eu_council1" == "median" {
		replace EUCOU_POS = eucou_median			if g105<=tq(1987q2)
		replace european_council =	"$eu_council1"	if g105<=tq(1987q2)
	}
	else if "$eu_council1" == "unanimity" {
		replace EUCOU_POS = eucou_unanimity			if g105<=tq(1987q2)
		replace european_council = "$eu_council1"	if g105<=tq(1987q2)
	}
	else if "$eu_council1" == "power" {
		replace EUCOU_POS = eucou_power				if g105<=tq(1987q2)
		replace european_council = "$eu_council1:$pmweight"	if g105<=tq(1987q2)
	}


// Single European Act, cooperation (1987q3-1993q3)
	if 	"$eu_council2" == "mean" {
		replace EUCOU_POS = eucou_mean				if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_council = "$eu_council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$eu_council2" == "median" {
		replace EUCOU_POS = eucou_median			if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_council = "$eu_council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$eu_council2" == "unanimity" {
		replace EUCOU_POS = eucou_unanimity			if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_council = "$eu_council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$eu_council2" == "power" {
		replace EUCOU_POS = eucou_power				if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_council = "$eu_council2:$pmweight"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}


// Maastricht Treaty, co-decision I (1993q4 - 1999q1)
	if 	"$eu_council3" == "mean" {
		replace EUCOU_POS = eucou_mean				if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_council = "$eu_council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$eu_council3" == "median" {
		replace EUCOU_POS = eucou_median			if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_council = "$eu_council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$eu_council3" == "unanimity" {
		replace EUCOU_POS = eucou_unanimity			if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_council = "$eu_council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$eu_council3" == "power" {
		replace EUCOU_POS = eucou_power				if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_council = "$eu_council3:$pmweight"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}


// Amsterdam Treaty, co-decision II (1999q2-2009q4)
	if 	"$eu_council4" == "mean" {
		replace EUCOU_POS = eucou_mean				if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_council = "$eu_council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$eu_council4" == "median" {
		replace EUCOU_POS = eucou_median			if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_council = "$eu_council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$eu_council4" == "unanimity" {
		replace EUCOU_POS = eucou_unanimity			if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_council = "$eu_council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$eu_council4" == "power" {
		replace EUCOU_POS = eucou_power				if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_council = "$eu_council4:$pmweight"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}


// Lisbon Treaty, co-decision II with rotating presidency (2010q1- )
	if 	"$eu_council5" == "mean" {
		replace EUCOU_POS = eucou_mean				if g105 >=tq(2010q1)
		replace european_council = "$eu_council5"	if g105 >=tq(2010q1)
	}
	else if "$eu_council5" == "median" {
		replace EUCOU_POS = eucou_median			if g105 >=tq(2010q1)
		replace european_council = "$eu_council5"	if g105 >=tq(2010q1)
	}
	else if "$eu_council5" == "unanimity" {
		replace EUCOU_POS = eucou_unanimity			if g105 >=tq(2010q1)
		replace european_council = "$eu_council5"	if g105 >=tq(2010q1)
	}
	else if "$eu_council5" == "power" {
		replace EUCOU_POS = eucou_power				if g105 >=tq(2010q1)
		replace european_council = "$eu_council5:$pmweight"	if g105 >=tq(2010q1)
	}



* ----------------------
* Save temporary file

keep g101-g105 p611-p616 _____GOVERNMENT_POSITION-european_council
save tmp_eu_council, replace

capture: erase zs.dta
